<?php

namespace App\Service;

use App\Models\Base\LogVipSignUser;
use App\Models\Base\StatPlayer;
use App\Models\LoginLog\LogAccount;
use App\Models\Login\Account;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

/**
 * 玩家账号逻辑.
 */
class AccountService
{
    /**
     * [loginNumber 玩家登陆数量].
     *
     * @Author   王文凡
     * @DateTime 2018-04-26
     *
     * @version  1.0
     *
     * @return Collection
     */
    public static function loginNumber($start_time, $end_time, $channel_id = null)
    {
        $db = LogAccount::selectRaw('userid,account,time')
            ->whereBetween('time', [$start_time, $end_time]);
        if (null !== $channel_id) {
            $db = $db->where('channelId', $channel_id);
        }

        return $db->groupBy('userid')->get();
    }

    /**
     * 当日活跃用户.
     *
     * @return Collection
     */
    public static function todayActiveUser()
    {
        $time = $today = Carbon::today()->timestamp;

        return LogAccount::where('time', '>=', $time)->groupBy('userid')->get();
    }

    /**
     * [signNumber 新增用户].
     *
     * @Author   王文凡
     * @DateTime 2018-04-26
     *
     * @version  1.0
     *
     * @return [type] [description]
     */
    public static function signNumber($start_time, $end_time,$channel_id=null)
    {
        $db = Account::whereBetween('ac_createTime', [$start_time, $end_time]);
        if($channel_id){
            $db = $db->where('channelId',$channel_id);
        }
        return $db->get();
    }

    /**
     * [topTime 最高在线人数时段].
     *
     * @Author   王文凡
     * @DateTime 2018-04-27
     *
     * @version  1.0
     *
     * @param string $date_time 日期
     * @param string $order_    排序方式
     *
     * @return Model
     */
    public static function topTime($date_time)
    {
        $ret = StatPlayer::select(
            DB::Raw('sum(online_number) as numbers')
        )
            ->where('date_time', $date_time)
            ->groupBy('hour')->get();

        return $ret;
    }

    /**
     * [getHourPlayerNum 前一小时在线人数].
     *
     * @Author   王文凡
     * @DateTime 2018-04-27
     *
     * @version  1.0
     *
     * @param [type] $one_hour_ago [description]
     * @param [type] $now_hour     [description]
     *
     * @return [type] [description]
     */
    public static function getHourPlayerNum($one_hour_ago, $now_hour)
    {
        return LogAccount::whereBetween('time', [$one_hour_ago, $now_hour])->groupBy('userid')->get()->count();
    }

    /**
     * 获取账号渠道Id.
     *
     * @param int $accountId
     *
     * @return int
     */
    public static function getPlayerChannel($accountId)
    {
        $account = Account::where('ac_id', $accountId)->first();
        if ($account) {
            return $account->channelId;
        } else {
            return 0;
        }
    }

    /**
     * 根据用户id集合获取登录信息.
     *
     * @param array $userIds
     * @param null  $start_at
     * @param null  $end_at
     *
     * @return Collection
     */
    public static function loginByUserIds(array $userIds, $start_at = null, $end_at = null)
    {
        $db = LogAccount::whereIn('userid', arrayNumericString($userIds));
        if ($start_at && $end_at) {
            $db = $db->whereBetween('time', [$start_at, $end_at]);
        }

        return $db->groupBy('userid')->get();
    }

    /**
     * 某一日登录的vip用户.
     *
     * @param string $date
     * @param int    $channel_id
     *
     * @return Collection|null
     */
    public static function somedayLoginVipUser($date = '', $channel_id = null)
    {
        $db = LogVipSignUser::where('date_time', $date);
        if (null !== $channel_id) {
            $db = $db->where('channel_id', $channel_id);
        }

        return $db->get();
    }

    /**
     * 某一天之前注册的用户.
     *
     * @param $timestamp
     *
     * @return Collection|null
     */
    public static function somedayAgoUser($timestamp)
    {
        return Account::where('ac_createTime', '<=', $timestamp)->get();
    }
}
